class Issues
  attr_accessor :github, :events, :labels, :milestones

  def initialize(github)
    @github = github
    @comments = IssuesComments.new(github)
    @events = IssuesEvents.new(github)
    @labels = IssuesLabels.new(github)
    @milestones = IssuesMilestones.new(github)
  end

  def listIssues(filter='all', state='open', labels=nil,
      sort='created', direction='desc', since=nil)
    params = {
        :filter => filter,
        :state => state,
        :labels => labels,
        :sort => sort,
        :direction => direction,
        :since => since
    }
    params = @github.removeEmptyParams(params)
    url = 'issues?%s' % @github.parameterize(params)
    @github.get(url)
  end

  def listRepoIssues(repo, milestone=nil, assignee=nil,
      mentioned=nil, state='open', labels=nil,
      sort='created', direction='desc', since=nil,
      user=nil, filter='all')
    params = {
        :filter => filter,
        :state => state,
        :assignee => assignee,
        :mentioned => mentioned,
        :labels => labels,
        :sort => sort,
        :direction => direction,
        :since => since,
        :milestone => milestone
    }
    params = @github.removeEmptyParams(params)
    url = 'repos/%s/%s/issues?%s' % [user, repo, @github.parameterize(params)]
    @github.get(url)
  end

  def getIssue(repo, number, user=nil)
    username = user == nil ? @github.username : user
    @github.get('repos/%s/%s/issues/%s' % [username, repo, number])
  end

  def createIssue(repo, title, body=nil, assignee=nil,
      milestone=nil, labels=nil, user=nil)
    params = {
        :title => title,
        :body => body,
        :assignee => assignee,
        :milestone => milestone,
        :labels => labels
    }
    params = @github.removeEmptyParams(params)
    data = params.to_json
    @github.post('repos/%s/%s/issues' % [user, repo], data)
  end

  def editIssue(repo, id, title=nil, body=nil, assignee=nil,
      state=nil, milestone=nil, labels=nil,
      user=nil)
    username = user == nil ? @github.username : user
    params = {
        :title => title,
        :body => body,
        :assignee => assignee,
        :state => state,
        :milestone => milestone,
        :labels => labels
    }
    params = @github.removeEmptyParams(params)
    data = params.to_json
    @github.post('repos/%s/%s/issues/%s' % [username, repo, id], data)
  end
end


